/**
 * Copyright (C) 2005 the Lexi Project.
 *
 * This file is part of the Lexi document editor.
 *
 * Lexi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * Lexi is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GNU Classpath; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 *
 * Linking this library statically or dynamically with other modules is
 * making a combined work based on this library.  Thus, the terms and
 * conditions of the GNU General Public License cover the whole combination.
 */

package com.jmonkey.office.lexi.support;

import java.awt.Insets;
import java.beans.PropertyChangeListener;
import java.util.Hashtable;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JToolBar;

public final class ActionToolBar extends JToolBar {
  /* registry of listeners created for Action-JButton
   * linkage.  This is needed so that references can
   * be cleaned up at remove time to allow GC.
   */
  private static Hashtable s_listenerRegistry = null;
  
  /**
   * Add a new JButton which dispatches the action.
   *
   * @param action the Action object to add as a new menu item
   */
  public JButton add(Action action) {
    JButton button = new JButton((String) action.getValue(Action.NAME), 
                                 (Icon) action.getValue(Action.SMALL_ICON));
    button.setHorizontalTextPosition(JButton.CENTER);
    button.setVerticalTextPosition(JButton.BOTTOM);
    button.setEnabled(action.isEnabled());
    button.addActionListener(action);
    add(button);
    // FIXME - according the javadoc for the following method, we should be
    // doing this.  crawley 2005-08-24
    PropertyChangeListener listener = createActionChangeListener(button);
    addToRegistry(button, listener, action);
    action.addPropertyChangeListener(listener);
    return button;
  }
  
  private void addToRegistry(JButton button, PropertyChangeListener listener,
                             Action action) 
  {
    if (s_listenerRegistry == null) {
      s_listenerRegistry = new Hashtable();
    }
    if (listener != null) {
      s_listenerRegistry.put(button, listener);
      s_listenerRegistry.put(listener, action);
    }
  }
  
  /**
   * Add a new JButton which dispatches the action.
   *
   * @param action the Action object to add as a new menu item
   * @param showText true if the button should show the action text.
   */
  public JButton add(boolean showText, Action action) 
  {
    JButton button = showText ? 
        new JButton((String) action.getValue(Action.NAME), 
                    (Icon) action.getValue(Action.SMALL_ICON)) : 
        new JButton((Icon) action.getValue(Action.SMALL_ICON));
    if (showText){
      button.setHorizontalTextPosition(JButton.CENTER);
      button.setVerticalTextPosition(JButton.BOTTOM);
    } 
    else {
      button.setMargin(new Insets(0, 0, 0, 0));
    }
    button.setEnabled(action.isEnabled());
    button.addActionListener(action);
    add(button);
    // FIXME - according the javadoc for the following method, we should be
    // doing this.  crawley 2005-08-24
    PropertyChangeListener listener = createActionChangeListener(button);
    addToRegistry(button, listener, action);
    action.addPropertyChangeListener(listener);
    return button;
  }
}
